﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.Store;
using System.IO;

namespace Steve.Lucene
{
    public sealed class ByteArrayIndexInput : IndexInput
    {
        byte[] _data;
        long _position = 0;

        public ByteArrayIndexInput(byte[] data)
        {
            _data = data;

            if (_data == null) _data = new byte[] { };
        }

        public override void Close()
        {
            //nothing to do...
        }

        public override long GetFilePointer()
        {
            return _position;
        }

        public override long Length()
        {
            return _data.Length;
        }

        public override byte ReadByte()
        {
            if (_position + 1 > _data.Length) throw new IOException("EOF");

            var data = _data[_position];
            _position++;
            return data;
        }

        public override void ReadBytes(byte[] b, int offset, int len)
        {
            //TODO: not sure if this is correct, should it read what it can and not throw?
            if (_position + len > _data.Length) throw new IOException("EOF");

            Array.Copy(_data, _position, b, offset, len);

            _position += len;
        }

        public override void Seek(long pos)
        {
            if (pos + 1 > _data.Length) throw new IOException("EOF");

            _position = pos;
        }
    }
}
